package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {
	@Autowired
	private CartService cartService;
	
	@PostMapping("/cart/add")
	public ResultInfo save(@RequestBody Cart cart) {
		Cart cartFromDB = cartService.save(cart);
		return ResultInfo.success(cartFromDB);
	}
	
	@PostMapping("/cart/sub")
	public ResultInfo sub(@RequestBody Cart cart) {
		List<Cart> cartFromDB = cartService.sub(cart);
		return ResultInfo.success(cartFromDB);
	}
	@GetMapping("/cart/list")
	public ResultInfo findList() {
		List<Cart> cartList = cartService.findList();
		return ResultInfo.success(cartList);
	}
	
	@DeleteMapping("/cart/clean")
	public ResultInfo clean() {
		cartService.clean();
		return ResultInfo.success(null);
	}
}
